package com.sec.samsung.android.app.baymax.request.connection;

import org.apache.http.HttpResponse;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;

import android.app.Activity;
import android.util.Log;

import com.sec.samsung.android.app.baymax.R;
import com.sec.samsung.android.app.baymax.utils.DialogUtility;
import com.sec.samsung.android.app.baymax.utils.ProgressDialog;

/**
 * AsyncHttpResponseProcess: process server response
 * 
 * @author kieu.thang
 */
public class AsyncHttpResponseProcess implements AsyncHttpResponseListener {
    private static final String TAG = "AsyncHttpResponseProcess";

    private Activity context;
    private ProgressDialog progressDialog;

    public AsyncHttpResponseProcess(Activity context) {
        this.context = context;
    }

    @Override
    public void before() {
        // Show waiting dialog during connection
        progressDialog = new ProgressDialog(context);
        try {
            progressDialog.show();
        } catch (Exception e) {

        }
        progressDialog.setCancelable(false);

    }

    @Override
    public void after(int statusCode, HttpResponse response) {
        // Process server response
        if (progressDialog != null)

        {
            progressDialog.dismiss();
            progressDialog = null;
        }

        switch (statusCode) {
        case AsyncHttpBase.NETWORK_STATUS_OFF:
            try {
                DialogUtility.alert(context, context.getString(R.string.network_unvailable));
            } catch (Exception e) {
                DialogUtility.alert(context.getParent(), context.getString(R.string.network_unvailable));
                e.printStackTrace();
            }
            break;
        case AsyncHttpBase.NETWORK_STATUS_OK:
            processHttpResponse(response);
            break;
        default:
            //processHttpResponse(response);
            DialogUtility.alert(context, context.getString(R.string.failed_to_conect_server));
            break;
        }
    }

    /**
     * Process HttpResponse
     * 
     * @param response
     */
    public void processHttpResponse(HttpResponse response) {
        String json = "";
        try {
            // Get json response
            json = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);

            if (json == null) {
                DialogUtility.alert(context, "Can't extract server data");
                return;
            }

            processIfResponseSuccess(json);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    /**
     * Interface function
     * 
     * @throws JSONException
     */
    public void processIfResponseSuccess(String response) {
        Log.i(TAG, "Process if response is success ===================");
    }

    /**
     * Interface function
     */
    public void processIfResponseFail() {
        // SmartLog.log(TAG, "Process if response is fail ===================");
    }
}
